package com.zxc.oj.ojcodesandbox.docker;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.*;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.PullResponseItem;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.command.LogContainerResultCallback;

import java.util.List;

public class DockerDemo {
    public static void main(String[] args) throws InterruptedException {
        //获取默认的Docker Client
        DockerClient dockerClient = DockerClientBuilder.getInstance().build();
//        PingCmd pingCmd = dockerClient.pingCmd();
//        pingCmd.exec();
        //拉取镜像
        String image = "openjdk:8-alpine";
        PullImageCmd pullImageCmd = dockerClient.pullImageCmd(image);
        PullImageResultCallback callback = new PullImageResultCallback() {
            @Override
            public void onNext(PullResponseItem item) {
                System.out.printf("下载状态：" + item.getStatus());
                super.onNext(item);
            }
        };
        pullImageCmd.exec(callback).awaitCompletion();
        System.out.printf("下载完成");
        //创建容器
//        CreateContainerCmd containerCmd = dockerClient.createContainerCmd(image);
//        CreateContainerResponse containerResponse = containerCmd.withCmd("echo", "Hello Docker").exec();
//        System.out.printf(String.valueOf(containerResponse));

        //获取容器列表

//        ListContainersCmd listContainersCmd = dockerClient.listContainersCmd();
//        List<Container> exec = listContainersCmd.withShowAll(true).exec();
//        for (Container container : exec) {
//            System.out.println(container.toString());
//            System.out.println("===================");
//        }

//        String id="61560f0a1d96";
//        //启动容器
//        dockerClient.startContainerCmd(id).exec();
//
//         //获取日志
//
//        LogContainerResultCallback logContainerResultCallback = new LogContainerResultCallback() {
//            @Override
//            public void onNext(Frame item) {
//                System.out.printf(String.valueOf(item.getStreamType()));
//                System.out.printf("日志：" + new String(item.getPayload()));
//                super.onNext(item);
//            }
//        };
//        dockerClient.logContainerCmd(id).withStdOut(true).withStdErr(true).withFollowStream(true).exec(logContainerResultCallback).awaitCompletion();


    }
}
